package lab.gene.dao;

import java.util.List;

import lab.gene.pojo.GeneMinipre;
import lab.gene.pojo.Gene;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

@SuppressWarnings("deprecation")
public class GeneMiniPreDAO {
	private static SessionFactory factory = new AnnotationConfiguration().configure().addAnnotatedClass(GeneMinipre.class).addPackage("com.xyz").buildSessionFactory();

	@SuppressWarnings("unchecked")
	public GeneMinipre getGeneMinipreByNameAndType(Gene gene, String type) {
		GeneMinipre minipre= null;
		try {
			Session session = factory.openSession();
	        try {
	        	Query query = session.createQuery("select g from GeneMinipre as g where g.id = (select max(gg.id) from GeneMinipre as gg where gg.type = '"+type+"' and gg.gene=:gene)").setParameter("gene", gene);
	        	List<GeneMinipre> results = query.list();
	        	System.out.println("gene mini pre size:"+results.size());
	        	if (results.size() > 0) {
	        		minipre = results.get(0);
	        		System.out.println("gene mini pre found!"); 
	        	}
	        	else {
	        		System.out.println("result.size = "+ results.size());
	        		System.out.println("no gene mini pre found");
	        	}
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return minipre;
	}
	
	public void addGeneMinipre(GeneMinipre minipre) {
		try {
	         Session session = factory.openSession();
	         Transaction tx = null;
	         try {
	        	 tx = session.beginTransaction();
	        	 session.save(minipre); 
	        	 tx.commit();
	        	 } 
	         catch (HibernateException e) {
	        	 if (tx!=null) 
	        		 tx.rollback();
	        	 e.printStackTrace(); 
	         } 
	         finally {
	            session.close(); 
	         }
	      } catch (Throwable ex) { 
	         System.err.println("Failed to create sessionFactory object." + ex);
	         throw new ExceptionInInitializerError(ex); 
	      }
	}
	
	@SuppressWarnings("unchecked")
	public List<GeneMinipre> getGeneMinipreHistory(Gene gene, String type) {
		List<GeneMinipre> list = null;
		try {
			Session session = factory.openSession();
	        try {
	        	Query query = session.createQuery("select g from GeneMinipre as g where g.type = '"+type+"' and g.gene=:gene order by id asc").setParameter("gene",gene); 
	        	List<GeneMinipre> results = query.list();
	        	if (results.size() > 0) {
	        		list = results;
		        }
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return list;
	}
	

}
